package java_thinking.unit_14._14_3;

import java_thinking.unit_14.pets.Pet;
import java_thinking.unit_14.pets.PetCreator;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :
 * @author: cww
 * @DateTime: 2019/3/28 19:12
 */

public class ForNameCreator extends PetCreator {
    private static List<Class<? extends Pet>> types = new ArrayList<Class<? extends Pet>>();

    @Override
    public List<Class<? extends Pet>> types() {
        return types;
    }
    private static String[] typeNames = {
        "java_thinking.unit_14.pets.Mutt",
        "java_thinking.unit_14.pets.Pug",
        "java_thinking.unit_14.pets.EgyptianMau",
        "java_thinking.unit_14.pets.Manx",
        "java_thinking.unit_14.pets.Cymric",
        "java_thinking.unit_14.pets.Rat",
        "java_thinking.unit_14.pets.Mouse",
        "java_thinking.unit_14.pets.Hamster"
    };
    @SuppressWarnings("unchecked")
    private static void loader() {
        try {
            for (String name : typeNames){
                //为了产生具有实际类型的Class对象的List，必须使用转型，这会产生编译期警告
                types.add((Class<? extends Pet>)Class.forName(name));
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException();
        }
    }
    static {
        loader();
    }
}
